javascriptfandomcom-20200222-history
String
A string is a sequence of textual characters (like this article). Internally, the characters are represented by numbers. Literal Syntax Definition A literal string is defined by enclosing a sequence of characters in either single quotes or double quotes. They are equivalent, but for that single-quoted strings cannot contain an unescaped single quote, and double-quoted strings cannot contain an unescaped double quote. To create a string containing a quotation mark of the type used to delimit the string, the quotation mark must be escaped. // The following two are equivalent. "foo" 'foo' // These are illegal and will cause an error: // "foo"bar" // 'foo'bar' // But these are valid equivalents. 'foo"bar' "foo'bar" Escape Sequences A string may contain any number of escape sequences. These represent one character each, and are usually used to include special characters in the string that would otherwise break the syntax. They are formed of the backslash character (\) followed by a character or sequence of characters specifying the character to be inserted. Using the escaped quotation marks, the above invalid examples can be written validly like so: "foo\"bar" 'foo\'bar' Operators Combination Operators These operators take two strings and return a new string. + (Concatenation) Creates a new string consisting of the characters of the left operand followed by the characters of the right operand. Beware: if either operand is a string, the other will always be coerced into a string: the string will never be coerced. "foo" + "bar" // "foobar" 5 + "baz" // "5baz" "baz" + 5 // "baz5" "4" + 5 // "45" 4 + "5" // "45" Comparison Operators Comparison operators take two strings and return a boolean value. Strings are compared by their character codes, with earlier characters being weighted higher than later characters. (Equals) Compares two strings for equality. Values of different types are automatically considered unequal. "2" 2 // false "foo" "foo" // true ! (Not Equals) Compares two values for inequality; x ! y is equivalent to !(x y). "foo" ! "bar" // true > (Greater Than) Compares two values and returns true if the first is greater than the second. "b" > "a" // true "b" > "b" // false "b" > "c" // false "bb" > "b" // true "bb" > "ba" // true "bb" > "bc" // false < (Less Than) Less-than. Opposite of greater-than. a < b is equivalent to b > a. "ba" < "bb" // true >= (Greater Than Or Equal To) Similar to greater-than, but also returns true if the values are equal: a >= b is equivalent to a > b || a b. "bb" >= "ba" // true "bb" >= "bb" // true "bb" >= "bc" // false <= (Less Than Or Equal To) The opposite of >=. a <= b is equivalent to a < b || a b. "bb" <= "ba" // false "bb" <= "bb" // true "bb" <= "bc" // true Members prototype |documentation=Mozilla, Microsoft }} (This includes string literals.) split |documentation=Mozilla Microsoft }} Splits a String object into an array of strings by separating the string into substrings. separator Optional. Specifies the character to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string. limit Optional. Integer specifying a limit on the number of splits to be found.